<?php
class CBuyer extends CMember{
	
	protected $_name;
	
	protected $_primary;
	

	public function _setup(){
		
		$this->_name=R_PRE."buyers";
		
		$this->_primary="uid";
		
		parent::_setup();
		
	}
	
	public function create($commonData,$buyerData=null){
	
		$uid=parent::create($commonData);
		
		$this->_name=R_PRE."buyers";
		
		$this->_primary="uid";
		
		$buyerData['uid']=$uid;
		
		$this->insert($buyerData);
		
		return $uid;

	}
	
	public function login($username,$userpass,$member_type="buyer"){
	
		parent::login($username,$userpass,$member_type);
	
	}
	
	public function getUserDetailsUnion($fieldArray){
		
		$db=$this->getAdapter();
		
		$where=array();
		
		foreach ($fieldArray as $field => $value){
			
			$where[]= $db->quoteInto("$field = ?", $value);
			
		}
		
		$where=join(' AND ',$where);

		$select = $db->select();
		
		$select->from(R_PRE.'buyers', '*');
		
		$select->join(R_PRE.'members',R_PRE.'buyers.uid='.R_PRE.'members.uid','*');
		
		$select->where($where);
		
		return $db->fetchAll($select);
		
	}
	
	public function getUserDetails($fieldArray=null){
		
		$this->_name=R_PRE."buyers";
		
		$this->_primary="uid";
		
		$db=$this->getAdapter();
		
		$where=array();
		
		foreach ($fieldArray as $field => $value){
			
			$where[]= $db->quoteInto("$field = ?", $value);
			
		}
		
		$where=join(' AND ',$where);
		
		$row=$this->fetchAll($where);
		
		if($row!=null){
	
			return $row->toArray();
			
       	}else{
       		
       		return array();
       		
       	}
		
	}
	
	public function updateUserDetail($valueArray,$fieldArray){
		
		$this->_name=R_PRE."buyers";
		
		$this->_primary="uid";
		
		$db=$this->getAdapter();
		
		$where=array();
		
		foreach ($fieldArray as $field => $value){
			
			$where[]= $db->quoteInto("$field = ?", $value);
			
		}
		
		$where=join(' AND ',$where);
		
		return $this->update($valueArray,$where);
		
	}
	

	
	public function addGoodCredit($uid){
		
		$db=$this->getAdapter();
		
		try{
			
			$db->query("UPDATE ".R_PRE."buyers SET good_credit=good_credit+1 WHERE uid='$uid'");
			
			return true;
			
		}catch(Exception $e){
			
			return false;
			
		}
		
	}
	
	public function addBadCredit($uid){
		
		$db=$this->getAdapter();
		
		try{
			
			$db->query("UPDATE ".R_PRE."buyers SET bad_credit=bad_credit+1 WHERE uid='$uid'");
			
			return true;
			
		}catch(Exception $e){
			
			return false;
			
		}
		
	}

	
}